package com.todo.sight.application.backend.service.impl;

import com.alibaba.fastjson.JSONObject;
import com.todo.common.common.entity.response.BizPageResponse;
import com.todo.common.common.entity.response.BizResponse;
import com.todo.common.common.entity.response.page.PageResult;
import com.todo.sight.application.backend.service.SightInfoBackendService;
import com.todo.sight.application.convert.SightInfoConvert;
import com.todo.sight.application.entity.bo.SightBaseBO;
import com.todo.sight.application.entity.bo.SightDetailBO;
import com.todo.sight.application.entity.bo.SightPoiBO;
import com.todo.sight.application.entity.condition.SightQueryConditionBO;
import com.todo.sight.application.iface.backend.request.*;
import com.todo.sight.application.iface.backend.response.QuerySightInfoByConditionResp;
import com.todo.sight.application.iface.backend.response.SightInfoDetailResp;
import com.todo.sight.application.service.SightPoiService;
import com.todo.sight.application.service.SightService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.stereotype.Service;

import javax.annotation.Resource;

/**
 * (SightBase)表服务实现类
 *
 * @author zhangwenxiang
 * @since 2024-03-04 00:19:48
 */
@Slf4j
@Service("sightInfoBackendService")
public class SightInfoBackendServiceImpl implements SightInfoBackendService {

    @Resource
    private SightService sightService;
    @Resource
    private SightPoiService sightPoiService;

    @Override
    public BizResponse<SightInfoDetailResp> querySightDetailById(QuerySightDetailByIdReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.paramError("参数异常");
        }
        SightDetailBO sightDetailBO = sightService.querySightDetailById(req.getSightId());
        if (sightDetailBO == null) {
            log.warn("SightInfoBackendService#querySightDetailById 根据主键查询景点详情完成，数据为空 req：【{}】", JSONObject.toJSONString(req));
            return BizResponse.operationFailed("景点详情不存在");
        }
        return BizResponse.success(SightInfoConvert.convertToSightInfoDetailResp(sightDetailBO));
    }

    @Override
    public BizPageResponse<QuerySightInfoByConditionResp> querySightInfoByCondition(QuerySightInfoByConditionReq req) {
        if (req == null || !req.baseCheck()) {
            return BizPageResponse.paramError("参数异常");
        }
        SightQueryConditionBO conditionBO = SightInfoConvert.convertToSightQueryConditionBO(req);
        PageResult<SightBaseBO> pageResult = sightService.querySightBaseListByCondition(conditionBO);
        return BizPageResponse.success(SightInfoConvert.convertSightInfoResponseList(pageResult));
    }

    @Override
    public BizResponse<Boolean> insertSightDetail(InsertSightDetailReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.paramError("参数异常");
        }
        SightPoiBO sightPoiBO = sightPoiService.querySightPoiById(req.getPoiId());
        if (sightPoiBO == null) {
            log.warn("SightInfoBackendService#insertSightInfo 根据poiId查询poi信息完成，poi信息不存在 req：【{}】", JSONObject.toJSONString(req));
            return BizResponse.operationFailed("poi信息不存在");
        }
        SightDetailBO alreadyBO = sightService.querySightDetailByPoi(req.getPoiId());
        if (alreadyBO != null) {
            log.warn("SightInfoBackendService#insertSightInfo 根据poiId查询poi信息完成，poi信息已存在 req：【{}】", JSONObject.toJSONString(req));
            return BizResponse.operationFailed("poi信息已绑定，调整可以更新数据");
        }

        SightDetailBO sightDetailBO = SightInfoConvert.buildSightDetailBO(req, sightPoiBO);
        Boolean insertResult = sightService.insertSightDetail(sightDetailBO);
        return BizResponse.success(insertResult);
    }

    @Override
    public BizResponse<Boolean> updateSightDetailById(UpdateSightDetailReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.paramError("参数异常");
        }
        SightBaseBO sightBaseBO = sightService.querySightBaseById(req.getId());
        if (sightBaseBO == null) {
            log.warn("SightInfoBackendService#updateSightDetailById 根据主键查询景点信息完成，数据为空 req：【{}】", JSONObject.toJSONString(req));
            return BizResponse.operationFailed("景点信息不存在");
        }
        SightDetailBO sightDetailBO = SightInfoConvert.convertSightDetailBO(req);
        Boolean updateResult = sightService.updateSightDetail(sightDetailBO);
        return BizResponse.success(updateResult);
    }

    @Override
    public BizResponse<Boolean> removeSightInfoById(RemoveSightInfoByIdReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.paramError("参数异常");
        }
        Boolean removeResult = sightService.removeSightById(req.getId());
        return BizResponse.success(removeResult);
    }

    @Override
    public BizResponse<Boolean> enableSightInfoById(EnableSightInfoByIdReq req) {
        if (req == null || !req.baseCheck()) {
            return BizResponse.paramError("参数异常");
        }
        SightBaseBO sightBaseBO = sightService.querySightBaseById(req.getId());
        if (sightBaseBO == null) {
            log.warn("SightInfoBackendService#enableSightInfoById 根据主键查询景点信息完成，数据为空 req：【{}】", JSONObject.toJSONString(req));
            return BizResponse.operationFailed("景点信息不存在");
        }
        Boolean showResult = sightService.showSightById(req.getId(), req.getIsShow());
        return BizResponse.success(showResult);
    }
}
